﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Configuration;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;
using HMS.Resources;
using HMS.Data;

namespace HMS.Report.ReportUserControls
{
    /// <summary>
    /// Interaction logic for ScanSummaryByDoctorUserControl.xaml
    /// </summary>
    public partial class ScanSummaryByDoctorUserControl : UserControl
    {
        HMSEntities HMSContext;
        public ScanSummaryByDoctorUserControl()
        {
            InitializeComponent();
            HMSContext = new HMSEntities();
            InitializeControlState();
        }
        private void InitializeControlState()
        {
            DoctorAutocompleteTextBox.ItemsSource = HMSContext.Doctors;
            AsAtDateDatePicker.SelectedDate = DateTime.Now;
        }
        private bool ValidateControlValues()
        {
            if (DoctorAutocompleteTextBox.SelectedItem == null)
            {
                MessageBox.Show(HMS.Resources.Properties.Resources.DoctorNotSet, HMS.Resources.Properties.Resources.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Information);
                return false;
            }
            if (AsAtDateDatePicker.SelectedDate == null)
            {
                MessageBox.Show(HMS.Resources.Properties.Resources.AppoinmentDateNotSet, HMS.Resources.Properties.Resources.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Information);
                return false;

            }


            return true;
        }
        private void GenerateReport()
        {
            try
            {

                CrystalDecisions.CrystalReports.Engine.ReportClass crystalReport;
                System.Data.SqlClient.SqlDataAdapter sqlDataAdapter;
                crystalReport = new CrystalReports.ScanSummaryByDoctorReport();

                string tableName = "ScanSummaryByDoctorReport";
                string connectionString = ResourceManager.GetDefaultConnectionString();

                SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(connectionString);
                DataSet sqlDataSource = new System.Data.DataSet();

                Doctor selectedDoctor = DoctorAutocompleteTextBox.SelectedItem as Doctor;
                DateTime asAtDate = AsAtDateDatePicker.SelectedDate.Value;

                sqlDataAdapter = new System.Data.SqlClient.SqlDataAdapter(tableName, sqlConnection);
                sqlDataAdapter.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;
                sqlDataAdapter.SelectCommand.Parameters.AddWithValue("@DoctorID", selectedDoctor.DoctorID);
                sqlDataAdapter.SelectCommand.Parameters.AddWithValue("@AsAtDate", asAtDate);
                sqlDataAdapter.Fill(sqlDataSource, tableName);

                crystalReport.SetDataSource((System.Data.DataTable)sqlDataSource.Tables[tableName]);
                crystalReport.SetParameterValue("ReportAsAtDate", asAtDate);
                crystalReport.SetParameterValue("CompanyName", HMSContext.Settings.Where(s => s.SettingID == 1).First().Value);


                ReportViewer.ViewerCore.ReportSource = crystalReport;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, HMS.Resources.Properties.Resources.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Exclamation);

            }
        }
        private void ViewReportButton_Click(object sender, RoutedEventArgs e)
        {
            if (ValidateControlValues())
            {
                GenerateReport();
            }
        }
    }
}
